package beans;

import java.io.Serializable;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;

import javax.persistence.PersistenceUnit;
@ManagedBean
@SessionScoped
public class Authentication 
{
	
	public Authentication()
	{
		
	}
	
	
	@PersistenceUnit
	private EntityManagerFactory emf ;
	
	
	
	public boolean getAuthentication(Utilisateur user)
	{
		String login = user.getLogin();
		String mdp = user.getMotDePasse();
		emf = Persistence.createEntityManagerFactory("fr.dauphine.BanqueSec");
		EntityManager em = emf.createEntityManager();
		Utilisateur tmp = null;
		try 
		{
		 Query query  = em.createQuery("SELECT u FROM Utilisateur u where u.login=:login and u.motDePasse=:mdp");
		 query.setParameter("login", login);
		 query.setParameter("mdp", mdp);
		 tmp = (Utilisateur) query.getSingleResult();
		 em.close();
		 
		}
		catch(Exception e)
		{
			System.out.println("Requete failed");
		}
		
		 if (tmp == null)
		 {
		 return false;
		 }
		 else
		 {
			 return true;
		 }
		 
	}
	
	
	

}
